﻿using System;
using System.Text;
using IAM.Interface.Common.XMLResolution;
using IAM.Interface.Common.Encrypt;
using IAM.Interface.Common.Providers;
using IAM.Interface.Model.Interface;
using System.Xml;

namespace IAM.Interface.Common.Context
{
    /// <summary>
    /// 请求端发送消息上下文控制
    /// </summary>
    public class RequestContextControl
    {
        #region 构造函数
        public RequestContextControl(RequestContextModel contextmodel)
        {
            this._contextmodel = contextmodel;
        }
        #endregion
        #region 属性
        private RequestContextModel _contextmodel = null;
        private XmlObjConversion _xmlobjconversion = null;
        private AbstractXMLResolution _abxmlresolution = null;
        private EncryptDecrypt _endecrypt = null;
        private IProvider _iprovider = null;

        /// <summary>
        /// 传送控制类实体对象
        /// </summary>
        public RequestContextModel ContextModel
        {
            get
            {
                return _contextmodel;
            }
        }
        /// <summary>
        /// xml obj交换对象
        /// </summary>
        public XmlObjConversion XmlObjConversion
        {
            get
            {
                return _xmlobjconversion;
            }
            set
            {
                _xmlobjconversion = value;

            }
        }
        /// <summary>
        /// xml 解析 打包
        /// </summary>
        public AbstractXMLResolution AbstractXMLResolution
        {
            set
            {
                _abxmlresolution = value;
            }
            get
            {
                return _abxmlresolution;
            }
        }
        /// <summary>
        /// 加密解密
        /// </summary>
        public EncryptDecrypt EncryptDecrypt
        {
            get
            {
                return _endecrypt;
            }
            set
            {
                _endecrypt = value;
            }
        }
        /// <summary>
        /// xml传送接口
        /// </summary>
        public IProvider IProvider
        {
            get
            {
                return _iprovider;

            }
            set
            {
                _iprovider = value;
            }
        }
        #endregion
        #region 发送消息
        /// <summary>
        /// 发送消息
        /// 1.将对象转换成为xml
        /// 2.拼完整包请求包
        /// 3.对信息加密
        /// 4. 发包收包
        /// </summary>
        /// <returns></returns>
        public string Send()
        {
            string xml = "";
            //1.将对象转换成为xml
            XmlObjConversion = new XmlObjConversion(ContextModel.ReturnType, (IConvertXml)ContextModel.Obj);
            xml = XmlObjConversion.GetXml();
            //2.拼完整包请求包
            AbstractXMLResolution = new Request(ContextModel.ActionType,
                ContextModel.OperationType,
                ContextModel.AppType, 
                xml);
            xml = AbstractXMLResolution.GetWholeXML();
            //4. 发包收包
            ProviderMgnt pm = new ProviderMgnt(ContextModel.AppType, xml);
            IProvider = pm.CreateProvider();
            //4.1对信息加密
            IProvider.XML = IProvider.EncryptDecrypt.Encrypt(IProvider.XML);
            this.EncryptDecrypt = IProvider.EncryptDecrypt;
            return IProvider.GetResponseStr();
        }
        #endregion
        #region 接收消息
        /// <summary>
        /// 接收消息
        /// 1.对xml解密
        /// 2.对xml进行解析
        /// 3.将xml序列化成对象
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public object Incept(string xml)
        {
            //1.对xml解密
            xml = EncryptDecrypt.Decrypt(xml);
            //2.对xml进行解析
            XMLResolutionMgnt xmlrm = new XMLResolutionMgnt(xml);
            xmlrm.ActionType = ContextModel.ActionType;

            AbstractXMLResolution = xmlrm.CreateXMLResolution();
            XmlNode xn=AbstractXMLResolution.GetCurrentNode();
            //3.将xml序列化成对象
            XmlObjConversion = new XmlObjConversion(xmlrm.ActionType, AbstractXMLResolution.XmlDocument.DocumentElement.Name);
            XmlObjConversion.CurrentXmlNode = xn;
            return XmlObjConversion.ConvertToObj();
        }
        #endregion
    }
}
